package site.luojie;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.I0Itec.zkclient.ZkClient;

import static site.luojie.constant.ZkConfigConstant.CONFIG_PATH;
import static site.luojie.constant.ZkConfigConstant.CONFIG_ROOT_PATH;

/**
 * @author jie.luo
 * @since 2020-08-18
 */
public class InitZkConfig {
    public static void main(String[] args) throws Exception {

        ZkClient client = new ZkClient("test_server:2181");
        boolean root = client.exists(CONFIG_ROOT_PATH);
        if (!root) {
            client.createPersistent(CONFIG_ROOT_PATH);
        }
        boolean config = client.exists(CONFIG_PATH);
        if (!config) {
            client.createPersistent(CONFIG_PATH);
        }
        DataSourceInfo dataSource = new DataSourceInfo();
        dataSource.setUrl("jdbc:mysql://localhost:3306/example1?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");
        dataSource.setInitialSize(10);
        dataSource.setMinIdle(10);
        dataSource.setMaxActive(50);

        ObjectMapper objectMapper = new ObjectMapper();
        String string = objectMapper.writeValueAsString(dataSource);
        client.writeData(CONFIG_PATH, string);

    }
}
